package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.model.Noticetype;
import cn.edu.cqupt.cxcy.util.Validate;

/**
 * 一条新闻的详细信息 
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class IndexNewsInfo  extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String id  = req.getParameter("id");
		String pjax_back = "$(function(){$(\"a.pjax[href='index_news']\").trigger('click');});";
		if(req.getHeader("x-pjax") == null || "null".equals(req.getHeader("x-pjax"))) {
			req.setAttribute("pjax_back", pjax_back);
			List<Noticetype> nt = new IndexImpl().noticeTypes();
			req.setAttribute("nt", nt);
			req.getRequestDispatcher("html/index.jsp").forward(req, resp);
			return;
		}  else {
			List<Object> news = null;
			String error = "";
			if(!Validate.stringIsNO(id)) {
				error = "格式有误！";
			}
			int newsid = Integer.valueOf(id);//项目id
			news = new IndexImpl().getNoticeInfo(newsid);
			if(news == null || news.isEmpty()) {
				error = "公告不存在！";
			} else {
				new IndexImpl().addNewsView(newsid);//添加公告浏览次数
			}
			req.setAttribute("notice", news);
			req.setAttribute("error", error);
			req.getRequestDispatcher("html/news_gg_fy.jsp").forward(req, resp);
		}
	}
}
